package com.example.demo.activeMQ;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class Producer {

    private static final String url = "tcp://127.0.0.1:61616";//actvemq的服务器tcp连接方式
    private static final String queueName = "queue-test";//定义队列的名称

    public static void main(String[] args) throws JMSException {
        //实例化工厂
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        //通过连接工厂来获取连接
        Connection connection = connectionFactory.createConnection();
        //启动连接
        connection.start();
        //创建session,第一个参数为是否开启事务
        Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        //创建消息队列
        Destination destination = session.createQueue(queueName);
        //创建消息生产者
        MessageProducer messageProducer = session.createProducer(destination);

        for (int i = 0; i < 100; i++) {
            TextMessage textMessage = session.createTextMessage("test" + i);
             //7.发送消息
            messageProducer.send(textMessage);
            System.out.println("发送消息" + textMessage.getText());
        }
        connection.close();
    }


}
